package com.jadcon.checkbookregister.web;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.checkbookregister.domain.Entry;
import com.jadcon.checkbookregister.domain.EntryRepository;
import com.jadcon.checkbookregister.domain.EntryService;

@Controller
@SessionAttributes("newEntry")
public class BasicController {
	
	@Autowired()
	private EntryService entryService;
	
	@RequestMapping("/index.html")
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView("index");
		return mav;
	}
	
	@RequestMapping("/total.html")
	public ModelAndView total (@RequestParam String checkNumber, @RequestParam String name, @RequestParam String amount) {
		ModelAndView mav = new ModelAndView("total");
		Entry newEntry = new Entry();
		newEntry.setNumber(checkNumber);
		newEntry.setName(name);
		newEntry.setAmount(amount);
		entryService.save(newEntry);
		List<Entry> entryList = entryService.getList();
		if (entryList.size()==0){
			System.out.println("List is empty");
		} else {
			System.out.println("List is not empty");
		}
		mav.addObject(entryList);
		return mav;
	}
	

}
